﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace BLL.Dal
{

    /// <summary>
    /// 数据访问类:tbMenuPower
    /// </summary>
    public partial class MenuPowerDAL
    {
        public MenuPowerDAL()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string powerCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbMenuPower");
            strSql.Append(" where powerCode=@powerCode ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@powerCode", OleDbType.VarChar,255)			};
            parameters[0].Value = powerCode;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.MenuPower model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbMenuPower(");
            strSql.Append("powerCode,powerName,parentCode,indexNum,isLeaf,isActive,menuCode,powerLevel,iconName)");
            strSql.Append(" values (");
            strSql.Append("@powerCode,@powerName,@parentCode,@indexNum,@isLeaf,@isActive,@menuCode,@powerLevel,@iconName)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@powerCode", OleDbType.VarChar,255),
					new OleDbParameter("@powerName", OleDbType.VarChar,255),
					new OleDbParameter("@parentCode", OleDbType.VarChar,255),
					new OleDbParameter("@indexNum", OleDbType.Integer,4),
					new OleDbParameter("@isLeaf", OleDbType.Boolean,1),
					new OleDbParameter("@isActive", OleDbType.Boolean,1),
					new OleDbParameter("@menuCode", OleDbType.VarChar,255),
					new OleDbParameter("@powerLevel", OleDbType.Integer,4),
					new OleDbParameter("@iconName", OleDbType.VarChar,255)};
            parameters[0].Value = model.powerCode;
            parameters[1].Value = model.powerName;
            parameters[2].Value = model.parentCode;

            int indexNum = GetIndexNum(model.parentCode) + 1;
            parameters[3].Value = indexNum;
            parameters[4].Value = model.isLeaf;
            parameters[5].Value = model.isActive;
            parameters[6].Value = model.menuCode;
            parameters[7].Value = model.powerLevel;
            parameters[8].Value = model.iconName;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                //更新上级节点的叶子节标识
                if (model.parentCode.Trim() != "Root")
                {
                    SetIsLeaf(model.parentCode, false);
                }

                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取当前父节点的所有孩子数
        /// </summary>
        /// <param name="parentCode">父级节点标识</param>
        /// <returns></returns>
        public int GetIndexNum(string parentCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from tbMenuPower ");
            strSql.Append(" where parentCode=@parentCode ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@parentCode", OleDbType.VarChar,255)};

            parameters[0].Value = parentCode;

            object obj = DbHelperOleDb.GetSingle(strSql.ToString(), parameters);

            if (obj != null)
            {
                return (int)obj;
            }
            return 0;
        }


        /// <summary>
        /// 设置节点的叶子标识
        /// </summary>
        /// <param name="powerCode">菜单标识</param>
        /// <param name="blLeaf">是否叶子标识</param>
        /// <returns>执行成功</returns>
        public bool SetIsLeaf(string powerCode, bool blLeaf)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbMenuPower set ");
            strSql.Append("isLeaf=@isLeaf ");
            strSql.Append(" where powerCode=@powerCode ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@isLeaf", OleDbType.Boolean,1),
					new OleDbParameter("@powerCode", OleDbType.VarChar,255)};

            parameters[0].Value = blLeaf;
            parameters[1].Value = powerCode;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.MenuPower model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbMenuPower set ");
            strSql.Append("powerName=@powerName,");
            strSql.Append("parentCode=@parentCode,");
            strSql.Append("indexNum=@indexNum,");
            strSql.Append("isLeaf=@isLeaf,");
            strSql.Append("isActive=@isActive,");
            strSql.Append("menuCode=@menuCode,");
            strSql.Append("powerLevel=@powerLevel,");
            strSql.Append("iconName=@iconName");
            strSql.Append(" where powerCode=@powerCode ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@powerName", OleDbType.VarChar,255),
					new OleDbParameter("@parentCode", OleDbType.VarChar,255),
					new OleDbParameter("@indexNum", OleDbType.Integer,4),
					new OleDbParameter("@isLeaf", OleDbType.Boolean,1),
					new OleDbParameter("@isActive", OleDbType.Boolean,1),
					new OleDbParameter("@menuCode", OleDbType.VarChar,255),
					new OleDbParameter("@powerLevel", OleDbType.Integer,4),
					new OleDbParameter("@iconName", OleDbType.VarChar,255),
					new OleDbParameter("@powerCode", OleDbType.VarChar,255)};
            parameters[0].Value = model.powerName;
            parameters[1].Value = model.parentCode;
            parameters[2].Value = model.indexNum;
            parameters[3].Value = model.isLeaf;
            parameters[4].Value = model.isActive;
            parameters[5].Value = model.menuCode;
            parameters[6].Value = model.powerLevel;
            parameters[7].Value = model.iconName;
            parameters[8].Value = model.powerCode;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string powerCode)
        {
            Model.MenuPower delModel = GetModel(powerCode);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbMenuPower ");
            strSql.Append(" where powerCode=@powerCode ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@powerCode", OleDbType.VarChar,255)			};
            parameters[0].Value = powerCode;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                UpdateIndexNum(delModel.parentCode, delModel.indexNum);
                DelAllChild(powerCode);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新菜单的行号
        /// </summary>
        public void UpdateIndexNum(string nodeParentCode, int? nodeIndexNum)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update  tbMenuPower set indexNum=indexNum-1  ");
            strSql.Append(" where parentCode=@parentCode and indexNum>=@indexNum");
            OleDbParameter[] parameters = {
					new OleDbParameter("@menuCode", OleDbType.VarChar,255),	
                    new OleDbParameter("@indexNum", OleDbType.Integer)	};
            parameters[0].Value = nodeParentCode;
            parameters[1].Value = nodeIndexNum;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);

        }

        /// <summary>
        /// 删除所有子级节点
        /// </summary>
        /// <param name="strParentCode">父节点标识</param>
        public void DelAllChild(string strParentCode)
        {
            string strSql = "select * from tbMenuPower where parentCode=@parentCode";
            OleDbParameter[] parameters = {
					new OleDbParameter("@parentCode", OleDbType.VarChar,255)			};
            parameters[0].Value = strParentCode;

            DataTable dtAllChild = DbHelperOleDb.Query(strSql, parameters).Tables[0];

            for (int i = 0; i < dtAllChild.Rows.Count; i++)
            {
                StringBuilder strSqlTemp = new StringBuilder();
                strSqlTemp.Append("delete from tbMenuPower ");
                strSqlTemp.Append(" where powerCode=@powerCode ");
                OleDbParameter[] parametersTemp = {
					new OleDbParameter("@powerCode", OleDbType.VarChar,255)			};
                parameters[0].Value = dtAllChild.Rows[i]["powerCode"].ToString();

                int rows = DbHelperOleDb.ExecuteSql(strSqlTemp.ToString(), parameters);

                DelAllChild(dtAllChild.Rows[i]["powerCode"].ToString());
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string powerCodelist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbMenuPower ");
            strSql.Append(" where powerCode in (" + powerCodelist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.MenuPower GetModel(string powerCode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select powerCode,powerName,parentCode,indexNum,isLeaf,isActive,menuCode,powerLevel,iconName from tbMenuPower ");
            strSql.Append(" where powerCode=@powerCode ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@powerCode", OleDbType.VarChar,255)			};
            parameters[0].Value = powerCode;

            Model.MenuPower model = new Model.MenuPower();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.MenuPower DataRowToModel(DataRow row)
        {
            Model.MenuPower model = new Model.MenuPower();
            if (row != null)
            {
                if (row["powerCode"] != null)
                {
                    model.powerCode = row["powerCode"].ToString();
                }
                if (row["powerName"] != null)
                {
                    model.powerName = row["powerName"].ToString();
                }
                if (row["parentCode"] != null)
                {
                    model.parentCode = row["parentCode"].ToString();
                }
                if (row["indexNum"] != null && row["indexNum"].ToString() != "")
                {
                    model.indexNum = int.Parse(row["indexNum"].ToString());
                }
                if (row["isLeaf"] != null && row["isLeaf"].ToString() != "")
                {
                    if ((row["isLeaf"].ToString() == "1") || (row["isLeaf"].ToString().ToLower() == "true"))
                    {
                        model.isLeaf = true;
                    }
                    else
                    {
                        model.isLeaf = false;
                    }
                }
                if (row["isActive"] != null && row["isActive"].ToString() != "")
                {
                    if ((row["isActive"].ToString() == "1") || (row["isActive"].ToString().ToLower() == "true"))
                    {
                        model.isActive = true;
                    }
                    else
                    {
                        model.isActive = false;
                    }
                }
                if (row["menuCode"] != null)
                {
                    model.menuCode = row["menuCode"].ToString();
                }
                if (row["powerLevel"] != null && row["powerLevel"].ToString() != "")
                {
                    model.powerLevel = int.Parse(row["powerLevel"].ToString());
                }
                if (row["iconName"] != null)
                {
                    model.iconName = row["iconName"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select powerCode,powerName,parentCode,indexNum,isLeaf,isActive,menuCode,powerLevel,iconName ");
            strSql.Append(" FROM tbMenuPower ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbMenuPower ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.powerCode desc");
            }
            strSql.Append(")AS Row, T.*  from tbMenuPower T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbMenuPower";
            parameters[1].Value = "powerCode";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
